package handler

import (
	"google.golang.org/protobuf/reflect/protoreflect"
	"google.golang.org/protobuf/types/dynamicpb"
	"sen-golang-study/go_game_study/hero_story_go_server/biz_server/service/user/userdata"
	"sen-golang-study/go_game_study/hero_story_go_server/common/server_msg/proto_msg"
	"time"
)

// 移动到指令处理器
func handleUserMoveTo(ctx CmdContext, pbMsgObj *dynamicpb.Message) {
	if nil == ctx ||
		ctx.GetUserId() <= 0 {
		return
	}

	userMoveToCmd := &proto_msg.UserMoveToCmd{}

	pbMsgObj.Range(
		func(f protoreflect.FieldDescriptor, v protoreflect.Value) bool {
			userMoveToCmd.ProtoReflect().Set(f, v)
			return true
		},
	)

	// 获取用户数据
	user := userdata.GetUserGroup().GetByUserId(ctx.GetUserId())

	if nil == user.MoveState {
		user.MoveState = &userdata.MoveState{}
	}

	nowTime := time.Now().UnixMilli()

	user.MoveState.FromPosX = userMoveToCmd.MoveFromPosX
	user.MoveState.FromPosY = userMoveToCmd.MoveFromPosY
	user.MoveState.ToPosX = userMoveToCmd.MoveToPosX
	user.MoveState.ToPosY = userMoveToCmd.MoveToPosY
	user.MoveState.StartTime = nowTime

	userMoveToResult := &proto_msg.UserMoveToResult{
		MoveUserId:    ctx.GetUserId(),
		MoveFromPosX:  userMoveToCmd.MoveFromPosX,
		MoveFromPosY:  userMoveToCmd.MoveFromPosY,
		MoveToPosX:    userMoveToCmd.MoveToPosX,
		MoveToPosY:    userMoveToCmd.MoveToPosY,
		MoveStartTime: uint64(nowTime),
	}

	GetCmdContextGroup().Broadcast(userMoveToResult)
}
